﻿using BLL;
using Model;
using System;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditFund_DropList : FormPage
    {
        private QS_PlanDetail_DropLIstBLL dropBLL = new QS_PlanDetail_DropLIstBLL();

        protected override void BindPage()
        {
            if (FORM_IFEDIT)
            {
                var dropListType = Request.QueryString["TYPE"];
                switch (dropListType)
                {
                    case "1":
                        var entity1 = dropBLL.FindUSERCLASS(Convert.ToDecimal(FORM_KEY));
                        FORM_NAME.Text = entity1.C_NAME;
                        FORM_FundType.Text = entity1.C_REMARK;
                        FORM_DropListType.Text = dropListType;
                        break;//用户类别
                    case "2":
                        var entity2 = dropBLL.FindPAYMENTCATEGORY(Convert.ToDecimal(FORM_KEY));
                        FORM_NAME.Text = entity2.C_NAME;
                        FORM_FundType.Text = entity2.C_REMARK;
                        FORM_DropListType.Text = dropListType;
                        break;//款项类别
                    case "3":
                        var entity3 = dropBLL.FindPAYMENTPURPOSE(Convert.ToDecimal(FORM_KEY));
                        FORM_NAME.Text = entity3.C_NAME;
                        FORM_FundType.Text = entity3.C_REMARK;
                        FORM_DropListType.Text = dropListType;
                        break;//款项用途
                    case "4":
                        var entity4 = dropBLL.FindPROJECTNAME(Convert.ToDecimal(FORM_KEY));
                        FORM_NAME.Text = entity4.C_NAME;
                        FORM_FundType.Text = entity4.C_REMARK;
                        FORM_DropListType.Text = dropListType;
                        if (entity4.C_REMARK == "T1")
                        {
                            FORM_PRIVATE.Visible = true;
                            FORM_PRIVATE.SelectedValue = entity4.N_PRIVATE.ToString();
                        }
                        break;//项目名称
                    default:
                        break;
                }
            }
            else
            {
                FORM_NAME.Text = "";
                FORM_FundType.Text = "-1";
                FORM_DropListType.Text = "-1";
                FORM_DropListType.Text = "-1";
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (FORM_NAME.Text.TrimEnd() == "")
            {
                MessageBox.Show(this, "项目名称不能为空");
                return;
            }
            if (FORM_FundType.Text.TrimEnd() == "-1")
            {
                MessageBox.Show(this, "所属种类不能为空");
                return;
            }
            if (FORM_DropListType.Text.TrimEnd() == "-1")
            {
                MessageBox.Show(this, "项目类型不能为空");
                return;
            }
            if (FORM_IFEDIT)
            {
                var dropListType = Request.QueryString["TYPE"];
                bool istrue = false;
                switch (dropListType)
                {
                    case "1":
                        var entity1 = dropBLL.FindUSERCLASS(Convert.ToDecimal(FORM_KEY));
                        entity1.C_NAME = FORM_NAME.Text.Trim();
                        entity1.C_REMARK = FORM_FundType.Text;
                        istrue = dropBLL.UpdateUSERCLASS(entity1, CurrentUserName);
                        break;//用户类别
                    case "2":
                        var entity2 = dropBLL.FindPAYMENTCATEGORY(Convert.ToDecimal(FORM_KEY));
                        entity2.C_NAME = FORM_NAME.Text.Trim();
                        entity2.C_REMARK = FORM_FundType.Text;
                        istrue = dropBLL.UpdatePAYMENTCATEGORY(entity2, CurrentUserName);
                        break;//款项类别
                    case "3":
                        var entity3 = dropBLL.FindPAYMENTPURPOSE(Convert.ToDecimal(FORM_KEY));
                        entity3.C_NAME = FORM_NAME.Text.Trim();
                        entity3.C_REMARK = FORM_FundType.Text;
                        istrue = dropBLL.UpdatePAYMENTPURPOSE(entity3, CurrentUserName);
                        break;//款项用途
                    case "4":
                        var entity4 = dropBLL.FindPROJECTNAME(Convert.ToDecimal(FORM_KEY));
                        entity4.C_NAME = FORM_NAME.Text.Trim();
                        entity4.C_REMARK = FORM_FundType.Text;
                        entity4.N_PRIVATE = Convert.ToDecimal(FORM_PRIVATE.Text == null ? "" : FORM_PRIVATE.Text);
                        istrue = dropBLL.UpdatePROJECTNAME(entity4, CurrentUserName);
                        break;//项目名称
                    default:
                        break;
                }

                if (istrue)
                {
                    MessageBox.Show(this, "修改成功");
                }
                else
                {
                    MessageBox.Show(this, "修改失败");
                    return;
                }
            }
            else
            {
                var dropListType = FORM_DropListType.Text;
                bool istrue = false;
                decimal dropId = 0;
                switch (dropListType)
                {
                    case "1":
                        var entity1 = new QS_PLANDETAILS_USERCLASS();
                        entity1.C_NAME = FORM_NAME.Text.Trim();
                        entity1.C_REMARK = FORM_FundType.Text;
                        istrue = dropBLL.InsertUSERCLASS(entity1, CurrentUserName);
                        dropId = entity1.N_ID;
                        break;//用户类别
                    case "2":
                        var entity2 = new QS_PLANDETAILS_PAYMENTCATEGORY();
                        entity2.C_NAME = FORM_NAME.Text.Trim();
                        entity2.C_REMARK = FORM_FundType.Text;
                        istrue = dropBLL.InsertPAYMENTCATEGORY(entity2, CurrentUserName);
                        dropId = entity2.N_ID;
                        break;//款项类别
                    case "3":
                        var entity3 = new QS_PLANDETAILS_PAYMENTPURPOSE();
                        entity3.C_NAME = FORM_NAME.Text.Trim();
                        entity3.C_REMARK = FORM_FundType.Text;
                        istrue = dropBLL.InsertPAYMENTPURPOSE(entity3, CurrentUserName);
                        dropId = entity3.N_ID;
                        break;//款项用途
                    case "4":
                        var entity4 = new QS_PLANDETAILS_PROJECTNAME();
                        entity4.C_NAME = FORM_NAME.Text.Trim();
                        entity4.C_REMARK = FORM_FundType.Text;
                        entity4.N_PRIVATE = Convert.ToDecimal(FORM_PRIVATE.Text == null ? "" : FORM_PRIVATE.Text);
                        istrue = dropBLL.InsertPROJECTNAME(entity4, CurrentUserName);
                        dropId = entity4.N_ID;
                        break;//项目名称
                    default:
                        break;
                }
                if (istrue)
                {
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Modal/EditFund_DropList.aspx?IF_EDIT=TRUE&ID=" + dropId);
                }
                else
                {
                    MessageBox.Show(this, "新增失败");
                    return;
                }
            }
        }

        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"编辑资金列表设置\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加资金列表设置\");");
            }
        }

        /// <summary>
        /// 所属种类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void FORM_DropListType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FORM_DropListType.Text == "4" && FORM_FundType.Text == "T1")
            {
                FORM_PRIVATE.Visible = true;
            }
            else
            {
                FORM_PRIVATE.Visible = false;
            }
        }

        /// <summary>
        /// 项目类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void FORM_FundType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FORM_DropListType.Text == "4" && FORM_FundType.Text == "T1")
            {
                FORM_PRIVATE.Visible = true;
            }
            else
            {
                FORM_PRIVATE.Visible = false;
            }
        }
    }
}